﻿package com.reyco1.util
{
	/*
	imports
	*/
	import flash.display.DisplayObject;
	import com.reyco1.events.ButtonEvent;
	import com.reyco1.gadget.EnhancedButton;
	import flash.geom.Rectangle;
	/*
	class
	*/
	public class DisplayObjectUtil
	{
		/*
		variables
		*/
		protected static var objDisplayObject:*;
		protected static var objRestriction:Rectangle;
		protected static var button:EnhancedButton;
		/*
		static functions
		*/
		
		/**
		 *
		 * makes the specified display object draggable
		 * @param $displayObject : display object 
		 * 
		 */		
		public static function makeDragable($displayObject:*, $restrictions:Rectangle):void
		{
			DisplayObjectUtil.objDisplayObject = $displayObject;
			DisplayObjectUtil.objRestriction = $restrictions;
			DisplayObjectUtil.button = new EnhancedButton($displayObject);
			DisplayObjectUtil.addDragFunctions();
		}
		
		private static function addDragFunctions():void
		{			
			DisplayObjectUtil.button.addButtonListener(ButtonEvent.DOWN, DisplayObjectUtil.drag);
			DisplayObjectUtil.button.addButtonListener(ButtonEvent.UP, DisplayObjectUtil.release);
			DisplayObjectUtil.button.addButtonListener(ButtonEvent.RELEASE_OUTSIDE, DisplayObjectUtil.release)
		}
		
		private static function drag($event:ButtonEvent):void
		{
			DisplayObjectUtil.button.child.startDrag(false, DisplayObjectUtil.objRestriction);
		}
		
		private static function release($event:ButtonEvent):void
		{
			DisplayObjectUtil.button.child.stopDrag();
		}
	}
}